package com.swagger.wen.controller;

import com.swagger.wen.model.User;
import com.swagger.wen.Exception.MyException;
import com.swagger.wen.bean.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *   @Api：修饰整个类，描述Controller的作用
     @ApiOperation：描述一个类的一个方法，或者说一个接口
     @ApiParam：单个参数描述
     @ApiModel：用对象来接收参数
     @ApiProperty：用对象接收参数时，描述对象的一个字段
     @ApiResponse：HTTP响应其中1个描述
     @ApiResponses：HTTP响应整体描述
     @ApiIgnore：使用该注解忽略这个API
     @ApiError ：发生错误返回的信息
     @ApiImplicitParam：一个请求参数
     @ApiImplicitParams：多个请求参数
 */

/**
 * Created by Think on 2018/6/14.
 */
@Api(value = "第一个测试controller")
@RestController
@RequestMapping("/api/v1")
public class DemoController1 {

    private static Logger LOGGER = LoggerFactory.getLogger(DemoController1.class);


    @ApiOperation(value="测试接口", notes="hello world",httpMethod = "GET",response = String.class)
    @RequestMapping(value = "/helloworld",method = RequestMethod.GET)
    public String helloWorld(){
        LOGGER.info("hello world");
        LOGGER.error("buhaole");
        return "hello world";
    }

    /**
     * 获取用户列表
     * @return
     */
    @ApiOperation(value="获取用户列表", notes="获取用户列表", httpMethod = "GET", response = ResponseEntity.class)
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserList(){
        JsonResult jsonResult = new JsonResult();
        try {
            List<User> userList = new ArrayList<User>();
            User user = new User();
            user.setId(1);
            user.setName("张三");
            user.setAge(22);
            user.setTime(new Date());
            user.setComments("这是第一个用户");
            User user1 = new User();
            user1.setId(1);
            user1.setName("张三");
            user1.setAge(22);
            user1.setTime(new Date());
            user1.setComments("这是第一个用户");
            User user2 = new User();
            user2.setId(1);
            user2.setName("张三");
            user2.setAge(22);
            user2.setTime(new Date());
            user2.setComments("这是第一个用户");
            userList.add(user);
            userList.add(user1);
            userList.add(user2);
            jsonResult.setStatus("OK");
            jsonResult.setMsg("成功的返回");
            jsonResult.setResult(userList);
        } catch (MyException e) {
            jsonResult.setStatus("ERROR");
            jsonResult.setMsg("返回错误");
            jsonResult.setResult(e.getClass().getName()+":"+e.getMessage());
        }
        return ResponseEntity.ok(jsonResult);
    }

    /**
     * 根据ID查询用户
     * @param id
     * @return
     */
    @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
    @RequestMapping(value = "user/{id}", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> getUserById (@PathVariable(value = "id") Integer id){
        JsonResult r = new JsonResult();
        try {
            User user = new User();
            user.setId(1);
            user.setName("张三");
            user.setAge(22);
            user.setTime(new Date());
            user.setComments("这是第一个用户");
            r.setResult(user);
            r.setStatus("ok");
        } catch (MyException e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @ApiOperation(value="创建用户", notes="根据User对象创建用户")
    @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    @RequestMapping(value = "user", method = RequestMethod.POST)
    public ResponseEntity<JsonResult> add (@RequestBody User user){
        JsonResult r = new JsonResult();
        try {
            System.out.println("用户添加成功");
            r.setResult(user.getId());
            r.setStatus("ok");
        } catch (MyException e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")
    @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "user/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<JsonResult> delete (@PathVariable(value = "id") Integer id){
        JsonResult r = new JsonResult();
        try {
            System.out.println("删除这个用户成功");
            r.setResult(id);
            r.setStatus("ok");
        } catch (MyException e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");

            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    /**
     * 根据id修改用户信息
     * @param user
     * @return
     */
    @ApiOperation(value="更新信息", notes="根据url的id来指定更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "user", value = "用户实体user", required = true, dataType = "User")
    })
    @RequestMapping(value = "user/{id}", method = RequestMethod.PUT)
    public ResponseEntity<JsonResult> update (@PathVariable("id") Integer id, @RequestBody User user){
                JsonResult r = new JsonResult();
                try {
                    System.out.println("修改用户成功");
                    r.setResult(user);
                    r.setStatus("ok");
                } catch (MyException e) {
                    r.setResult(e.getClass().getName() + ":" + e.getMessage());
                    r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String  jsonTest() {
        return " hi you!";
    }

}
